package com.sugree.jibjib;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.sugree.jibjib.R;

public class LocaleReceiver extends BroadcastReceiver {
	private final String TAG = "LocaleReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
			final SharedPreferences.Editor esp = sp.edit();
			Log.d(TAG, intent.getAction());
			if (com.twofortyfouram.Intent.ACTION_FIRE_SETTING.equals(intent.getAction()) ||
				LocaleSettingActivity.ACTION_LOCALE_ALERT.equals(intent.getAction())) {
				applyFrequency(intent, esp,
					PreferencesActivity.KEY_FETCH_FRIENDS_FREQUENCY,
					LocaleSettingActivity.KEY_FETCH_FRIENDS_FREQUENCY_VALUE);
				applyFrequency(intent, esp,
					PreferencesActivity.KEY_FETCH_MENTIONS_FREQUENCY,
					LocaleSettingActivity.KEY_FETCH_MENTIONS_FREQUENCY_VALUE);
				applyFrequency(intent, esp,
					PreferencesActivity.KEY_FETCH_MESSAGES_FREQUENCY,
					LocaleSettingActivity.KEY_FETCH_MESSAGES_FREQUENCY_VALUE);
				esp.commit();
			}
		} catch (Exception ex) {
		}
	}

	protected void applyFrequency(Intent intent, SharedPreferences.Editor esp, String keyPref, String keyValue) {
		final int mFrequency = Integer.valueOf(intent.getStringExtra(keyValue));
		if (mFrequency > 0) {
			esp.putString(keyPref, String.valueOf(mFrequency));
		}
	}
}
